/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SongInfoDialog.java
 *
 * Created on 2011-5-29, 20:25:48
 */
package mp3tagger;

import entagged.audioformats.AudioFile;
import entagged.audioformats.AudioFileIO;
import entagged.audioformats.Tag;
import entagged.audioformats.exceptions.CannotReadException;
import entagged.audioformats.exceptions.CannotWriteException;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

/**
 *
 * @author Rajab
 */
public class SongInfoDialog extends javax.swing.JDialog {

    private File file;
    private AudioFile mp3;
    private Tag tag;

    /** Creates new form SongInfoDialog */
    public SongInfoDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void setFile(File file) {
        this.file = file;
        try {
            mp3 = AudioFileIO.read(file);
            tag = mp3.getTag();

            refresh();
        } catch (CannotReadException ex) {
            System.out.println(ex);
        }
    }

    public void show(File file) {
        setFile(file);
        setVisible(true);
    }

    public void refresh() {
        if (tag == null || tag.isEmpty()) {
            return;
        }
        txtArtist.setText(tag.getFirstArtist());
        txtTitle.setText(tag.getFirstTitle());
        txtFileName.setText(file.getAbsolutePath());
        txtTrackNumber.setText(tag.getFirstYear());
        txtGenre.setSelectedItem(tag.getFirstGenre());
        txtComment.setText(tag.getFirstComment());
        txtAlbum.setText(tag.getFirstAlbum());

        txtEncodingType.setText(mp3.getEncodingType().split(" ")[0]);
        txtSample.setText(String.format("%d Bits", mp3.getSamplingRate()));
        txtBitrate.setText(String.format("%d kbps", mp3.getBitrate()));
        txtChannelNumber.setText(String.format(("%d 声道"), mp3.getChannelNumber()));
        //txtFreq.setText(String.format(("%d 声道"), mp3.))
        int length = mp3.getLength();
        int min = length / 60;
        int sec = length % 60;
        txtLength.setText(String.format("%d:%d", min, sec));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tagPanel = new JPanel();
        lblAlbum = new JLabel();
        jScrollPane1 = new JScrollPane();
        txtComment = new JTextArea();
        lblGenre = new JLabel();
        txtArtist = new JTextField();
        txtTrackNumber = new JTextField();
        lblArtist = new JLabel();
        txtTitle = new JTextField();
        lbTitle = new JLabel();
        lblTrackNumber = new JLabel();
        txtAlbum = new JTextField();
        lblNote = new JLabel();
        lblYear = new JLabel();
        txtYear = new JTextField();
        txtGenre = new JComboBox();
        lblFileName = new JLabel();
        txtFileName = new JTextField();
        btnSave = new JButton();
        btnClose = new JButton();
        btnRead = new JButton();
        jPanel1 = new JPanel();
        lblEncodingType = new JLabel();
        txtEncodingType = new JTextField();
        lblChannelNumber = new JLabel();
        txtChannelNumber = new JTextField();
        txtFreq = new JLabel();
        jTextField3 = new JTextField();
        lblSample = new JLabel();
        txtSample = new JTextField();
        lblBitrate = new JLabel();
        txtBitrate = new JTextField();
        lblLength = new JLabel();
        txtLength = new JTextField();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("曲目属性");
        setResizable(false);

        tagPanel.setBorder(BorderFactory.createTitledBorder("标签"));
        tagPanel.setName("tagPanel"); // NOI18N

        lblAlbum.setText("专    辑:");
        lblAlbum.setName("lblAlbum"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtComment.setColumns(20);
        txtComment.setRows(5);
        txtComment.setName("txtComment"); // NOI18N
        jScrollPane1.setViewportView(txtComment);

        lblGenre.setText("流    派:");
        lblGenre.setName("lblGenre"); // NOI18N

        txtArtist.setName("txtArtist"); // NOI18N

        txtTrackNumber.setHorizontalAlignment(JTextField.RIGHT);
        txtTrackNumber.setName("txtTrackNumber"); // NOI18N

        lblArtist.setText("艺术家:");
        lblArtist.setName("lblArtist"); // NOI18N

        txtTitle.setName("txtTitle"); // NOI18N

        lbTitle.setText("标    题:");
        lbTitle.setName("lbTitle"); // NOI18N

        lblTrackNumber.setText("音轨:");
        lblTrackNumber.setName("lblTrackNumber"); // NOI18N

        txtAlbum.setName("txtAlbum"); // NOI18N

        lblNote.setText("备    注:");
        lblNote.setName("lblNote"); // NOI18N

        lblYear.setText("年代:");
        lblYear.setName("lblYear"); // NOI18N

        txtYear.setHorizontalAlignment(JTextField.RIGHT);
        txtYear.setName("txtYear"); // NOI18N

        txtGenre.setModel(new DefaultComboBoxModel(Tag.DEFAULT_GENRES));
        txtGenre.setName("txtGenre"); // NOI18N

        GroupLayout tagPanelLayout = new GroupLayout(tagPanel);
        tagPanel.setLayout(tagPanelLayout);
        tagPanelLayout.setHorizontalGroup(
            tagPanelLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(tagPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tagPanelLayout.createParallelGroup(Alignment.LEADING)
                    .addGroup(tagPanelLayout.createSequentialGroup()
                        .addGroup(tagPanelLayout.createParallelGroup(Alignment.LEADING, false)
                            .addComponent(lblArtist, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblAlbum)
                            .addComponent(lblGenre)
                            .addComponent(lbTitle, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(tagPanelLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(tagPanelLayout.createSequentialGroup()
                                .addGroup(tagPanelLayout.createParallelGroup(Alignment.LEADING, false)
                                    .addComponent(txtAlbum)
                                    .addComponent(txtGenre, 0, 192, Short.MAX_VALUE))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(tagPanelLayout.createParallelGroup(Alignment.TRAILING)
                                    .addComponent(lblTrackNumber)
                                    .addComponent(lblYear))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(tagPanelLayout.createParallelGroup(Alignment.LEADING)
                                    .addComponent(txtTrackNumber, GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                                    .addComponent(txtYear, GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)))
                            .addComponent(txtArtist, GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                            .addComponent(txtTitle, GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)))
                    .addGroup(tagPanelLayout.createSequentialGroup()
                        .addComponent(lblNote)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)))
                .addContainerGap())
        );
        tagPanelLayout.setVerticalGroup(
            tagPanelLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(tagPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tagPanelLayout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lbTitle)
                    .addComponent(txtTitle, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(tagPanelLayout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblArtist)
                    .addComponent(txtArtist, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(tagPanelLayout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblAlbum)
                    .addComponent(txtAlbum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTrackNumber)
                    .addComponent(txtTrackNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(tagPanelLayout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblGenre)
                    .addComponent(txtYear, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtGenre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblYear))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(tagPanelLayout.createParallelGroup(Alignment.LEADING)
                    .addComponent(lblNote)
                    .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        tagPanelLayout.linkSize(SwingConstants.VERTICAL, new Component[] {txtAlbum, txtArtist, txtTitle});

        lblFileName.setText("文件名：");
        lblFileName.setName("lblFileName"); // NOI18N

        txtFileName.setEditable(false);
        txtFileName.setName("txtFileName"); // NOI18N

        btnSave.setText("保存到文件");
        btnSave.setName("btnSave"); // NOI18N
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnClose.setText("关闭");
        btnClose.setName("btnClose"); // NOI18N
        btnClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnRead.setText("重新读取文件");
        btnRead.setName("btnRead"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        lblEncodingType.setText("编码:");
        lblEncodingType.setName("lblEncodingType"); // NOI18N

        txtEncodingType.setEditable(false);
        txtEncodingType.setName("txtEncodingType"); // NOI18N

        lblChannelNumber.setText("声道:");
        lblChannelNumber.setName("lblChannelNumber"); // NOI18N

        txtChannelNumber.setEditable(false);
        txtChannelNumber.setName("txtChannelNumber"); // NOI18N

        txtFreq.setText("频率:");
        txtFreq.setName("txtFreq"); // NOI18N

        jTextField3.setEditable(false);
        jTextField3.setName("jTextField3"); // NOI18N

        lblSample.setText("比特:");
        lblSample.setName("lblSample"); // NOI18N

        txtSample.setEditable(false);
        txtSample.setName("txtSample"); // NOI18N

        lblBitrate.setText("码率:");
        lblBitrate.setName("lblBitrate"); // NOI18N

        txtBitrate.setEditable(false);
        txtBitrate.setName("txtBitrate"); // NOI18N

        lblLength.setText("长度:");
        lblLength.setName("lblLength"); // NOI18N

        txtLength.setEditable(false);
        txtLength.setName("txtLength"); // NOI18N

        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
                    .addComponent(lblChannelNumber)
                    .addComponent(txtFreq)
                    .addComponent(lblSample)
                    .addComponent(lblEncodingType)
                    .addComponent(lblBitrate)
                    .addComponent(lblLength))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING, false)
                    .addComponent(txtEncodingType, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtChannelNumber, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSample, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtBitrate, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLength, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {jTextField3, txtBitrate, txtChannelNumber, txtEncodingType, txtLength, txtSample});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(txtEncodingType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblEncodingType))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblChannelNumber)
                    .addComponent(txtChannelNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(txtFreq)
                    .addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(txtSample, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSample))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblBitrate)
                    .addComponent(txtBitrate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblLength)
                    .addComponent(txtLength, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(SwingConstants.VERTICAL, new Component[] {jTextField3, txtBitrate, txtChannelNumber, txtEncodingType, txtLength, txtSample});

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(33, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnRead)
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addComponent(btnSave)
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addComponent(btnClose, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE))
                    .addGroup(Alignment.LEADING, layout.createParallelGroup(Alignment.TRAILING, false)
                        .addGroup(Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(lblFileName, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(ComponentPlacement.UNRELATED)
                            .addComponent(txtFileName))
                        .addComponent(tagPanel, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10)
                .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                    .addGroup(Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(txtFileName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblFileName))
                        .addPreferredGap(ComponentPlacement.UNRELATED)
                        .addComponent(tagPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(btnSave)
                    .addComponent(btnRead))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        setVisible(false);
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnSaveActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (mp3 == null) {
            return;
        }
        tag.setTitle(txtTitle.getText().trim());
        tag.setArtist(txtArtist.getText().trim());
        tag.setAlbum(txtAlbum.getText().trim());
        tag.setTrack(txtTrackNumber.getText().trim());
        tag.setYear(txtYear.getText().trim());
        tag.setComment(txtComment.getText().trim());
        tag.setGenre(txtGenre.getSelectedItem().toString());
        try {
            mp3.commit();
        } catch (CannotWriteException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "保存出错", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnSaveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                SongInfoDialog dialog = new SongInfoDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JButton btnClose;
    private JButton btnRead;
    private JButton btnSave;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JTextField jTextField3;
    private JLabel lbTitle;
    private JLabel lblAlbum;
    private JLabel lblArtist;
    private JLabel lblBitrate;
    private JLabel lblChannelNumber;
    private JLabel lblEncodingType;
    private JLabel lblFileName;
    private JLabel lblGenre;
    private JLabel lblLength;
    private JLabel lblNote;
    private JLabel lblSample;
    private JLabel lblTrackNumber;
    private JLabel lblYear;
    private JPanel tagPanel;
    private JTextField txtAlbum;
    private JTextField txtArtist;
    private JTextField txtBitrate;
    private JTextField txtChannelNumber;
    private JTextArea txtComment;
    private JTextField txtEncodingType;
    private JTextField txtFileName;
    private JLabel txtFreq;
    private JComboBox txtGenre;
    private JTextField txtLength;
    private JTextField txtSample;
    private JTextField txtTitle;
    private JTextField txtTrackNumber;
    private JTextField txtYear;
    // End of variables declaration//GEN-END:variables
}
